package com.ls.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tedu.crm.entity.Employee;
import cn.tedu.crm.service.IEmployeeService;
import cn.tedu.crm.util.EmpUtil;
import cn.tedu.crm.util.JsonResult;

@Controller
@RequestMapping("/emp")
public class EmployeeController {
	@Resource(name = "employeeService")
	private IEmployeeService service;

	@RequestMapping("/login.do")
	@ResponseBody
	public JsonResult<Employee> login(String email, String password,HttpSession session) {
		JsonResult<Employee> result = service.checkLogin(email, password);
		session.setAttribute("user", result.getData());
		return result;

	}
	@RequestMapping("/login2.do")
	@ResponseBody
	public JsonResult<Employee> login2(int emp_id,HttpSession session) {
		JsonResult<Employee> result = service.showEmp(emp_id);
		session.setAttribute("user", result.getData());
		Employee employee=(Employee) session.getAttribute("user");
		System.out.println(employee);
		return result;

	}

	@RequestMapping("/selectemp.do")
	@ResponseBody
	public JsonResult<List<Employee>> selectEmployee(@RequestParam(value = "pageId", defaultValue = "1") int pageId,
			@RequestParam(value = "num", defaultValue = "4") int num) {
		JsonResult<List<Employee>> result = service.findEmployeeFenye(pageId, num);
		return result;

	}

	@RequestMapping("/addemp.do")
	@ResponseBody
	public JsonResult<Object> addEmployee(String emp_name, int age, String email, String password, int dept_id,
			String hire_date, String info) {

		JsonResult<Object> result = service.insertEmployee(emp_name, age, email, password, dept_id, hire_date, info);
		return result;

	}

	@RequestMapping("/deleteid.do")
	@ResponseBody
	public JsonResult<Object> deleteEmployee(String[] emp_id) {
		JsonResult<Object> result = service.deleteByIdEmp(emp_id);
		return result;

	}

	@RequestMapping("/selectename.do")
	@ResponseBody
	public JsonResult<List<Employee>> selectName(String name,@RequestParam(value = "pageId", defaultValue = "1") int pageId,
			@RequestParam(value = "num", defaultValue = "4") int num) {
		JsonResult<List<Employee>> result = service.findName(name,pageId,num);
		return result;

	}
	
	@RequestMapping("/option.do")
	@ResponseBody
	public JsonResult<List<Employee>> Option() {
		JsonResult<List<Employee>> result = service.findAllEmployee();
		return result;

	}
	
	@RequestMapping("/show.do")
	@ResponseBody
	public JsonResult<Employee> showEmployee(int emp_id) {
		JsonResult<Employee> result = service.showEmp(emp_id);
		return result;
		
	}
	
	@RequestMapping("/update.do")
	@ResponseBody
	public JsonResult<Object> updateEmployee(Employee employee) {
		JsonResult<Object> result = service.updateEmployee(employee);
		return result;
		
	}
	
	@RequestMapping("/upinfo.do")
	@ResponseBody
	public JsonResult<Object> upinfo(String emp_name,String email,String info,int emp_id) {
		JsonResult<Object> result = service.upinfo(emp_id,emp_name,email,info);
		return result;
		
	}
	
	@RequestMapping("/uppwd.do")
	@ResponseBody
	public JsonResult<Object> uppwd(int emp_id,String password) {
		String pwd = EmpUtil.md5(password);
		JsonResult<Object> result = service.uppwd(emp_id,pwd);
		return result;
		
	}
	@RequestMapping("/selectpwd.do")
	@ResponseBody
	public JsonResult<Employee> selectpwd(int emp_id,String password) {
		JsonResult<Employee> result = service.selectpwd(emp_id, password);
		return result;
		
	}
	
	@RequestMapping("/out.do")
	public String out(HttpSession session) {
		session.invalidate();
		return "redirect:/index.jsp";
		
	}
	
	@RequestMapping("/aj.do")
	@ResponseBody
	public String aj(HttpSession session) {
		Employee user=(Employee)session.getAttribute("user");
		if (user!=null) {
			return "yes";
		}
		return "no";
		
	}

}
